SET SERVEROUTPUT ON SIZE 1000000;
ALTER TRIGGER GL_PERIOD_SETS_CT1 DISABLE;



DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_PERIOD_SETS';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_G IS
	SELECT
		*
	FROM
		CRST_GL_PERIOD_SETS;

BEGIN

	--  RENAME
	UPDATE
		CGL_PERIOD_SETS
	SET
		PERIOD_SET_NAME	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(PERIOD_SET_NAME,1,4),PERIOD_SET_NAME)
	WHERE	PERIOD_SET_NAME	IN	(
								SELECT
									PERIOD_SET_NAME
								FROM
									CRST_GL_PERIOD_SETS
								);

	 FOR C2 IN Cur_G LOOP
		BEGIN
	
			INSERT INTO CGL_PERIOD_SETS
			VALUES
			(
			C2.PERIOD_SET_NAME,
			CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
			CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
ALTER TRIGGER GL_PERIOD_SETS_CT1 ENABLE;
